<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
       <el-form-item label="户型图" prop="showImage">
        <UploadImg v-model="formData.showImage" />
      </el-form-item>
      <el-form-item label="户型" prop="houseType">
        <el-select
          v-model="formData.houseType"
          placeholder="请选择户型">
          <el-option v-for="dict in dictList" :key="dict.id" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="起售价格" prop="priceMin">
        <el-input-number style="width: 100%" :min="0.01" v-model="formData.priceMin" placeholder="请输入起售价格" />
      </el-form-item>
<!--      <el-form-item label="最高价格" prop="priceMax">
        <el-input-number style="width: 100%" :min="0.02" v-model="formData.priceMax" placeholder="请输入最高价格" />
      </el-form-item>-->
<!--      <el-form-item label="房屋面积" prop="houseArea">
        <el-input-number style="width: 100%" :min="0.01" v-model="formData.houseArea" placeholder="请输入房屋面积（单位：㎡）" />
      </el-form-item>-->
      <el-form-item label="车位" prop="parkingSpace">
        <el-input v-model="formData.parkingSpace" placeholder="请输入车位" />
      </el-form-item>
      <el-form-item label="室内面积" prop="indoorArea">
        <el-input-number style="width: 100%" :min="0.01" v-model="formData.indoorArea" placeholder="请输入室内面积（单位：㎡）" />
      </el-form-item>
      <el-form-item label="阳台面积" prop="balconyArea">
        <el-input-number style="width: 100%" :min="0.01" v-model="formData.balconyArea" placeholder="请输入阳台面积（单位：㎡）" />
      </el-form-item>
<!--      <el-form-item label="状态中文" prop="statusZhCn">
        <el-input v-model="formData.statusZhCn" placeholder="请输入状态中文" />
      </el-form-item>
      <el-form-item label="状态英文" prop="statusEnUs">
        <el-input v-model="formData.statusEnUs" placeholder="请输入状态英文" />
      </el-form-item>-->
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ApartmentApi } from '@/api/business/apartment'
import { BizDictTypeApi } from '@/api/business/dicttype'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  apartmentId: undefined,
  showImage: undefined,
  houseType: undefined,
  priceMin: undefined,
  priceMax: undefined,
  houseArea: undefined,
  statusZhCn: undefined,
  statusEnUs: undefined,
  parkingSpace: undefined,
  indoorArea: undefined,
  balconyArea: undefined,
})
const formRules = reactive({
  apartmentId: [{ required: true, message: '楼盘/公寓ID不能为空', trigger: 'blur' }],
  showImage: [{ required: true, message: '户型图不能为空', trigger: 'blur' }],
  houseType: [{ required: true, message: '户型不能为空', trigger: 'blur' }],
  priceMin: [{ required: true, message: '起售价格不能为空', trigger: 'blur' }],
  /*priceMax: [{ required: true, message: '最高价格不能为空', trigger: 'blur' }],*/
  houseArea: [{ required: true, message: '房屋面积不能为空', trigger: 'blur' }],
  statusZhCn: [{ required: true, message: '状态中文不能为空', trigger: 'blur' }],
  statusEnUs: [{ required: true, message: '状态英文不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

const dictList = ref() // 弹窗的是否展示
const getDictDataList = async (id: number) => {
  dictList.value = await BizDictTypeApi.getBizDictDataByTypeId(id)
}

/** 打开弹窗 */
const open = async (type: string, id?: number, apartmentId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  await getDictDataList(1);
  formData.value.apartmentId = apartmentId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ApartmentApi.getApartmentHouseType(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await ApartmentApi.createApartmentHouseType(data)
      message.success(t('common.createSuccess'))
    } else {
      await ApartmentApi.updateApartmentHouseType(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    apartmentId: undefined,
    showImage: undefined,
    houseType: undefined,
    priceMin: undefined,
    priceMax: undefined,
    houseArea: undefined,
    statusZhCn: undefined,
    statusEnUs: undefined,
    parkingSpace: undefined,
    indoorArea: undefined,
    balconyArea: undefined,
  }
  formRef.value?.resetFields()
}
</script>
